/*
 * @(#)CharacterEncodingFilter.java
 *
 * Copyright 2012 by ShenY BIS, All rights reserved.
 */

package com.bis.framework.filter;

import java.io.IOException;

import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;

/**
 * CharacterEncodingFilter
 * 
 * @author BIS 2012-12-24
 */
public class CharacterEncodingFilter extends TemplateFilter
{

    /**
     * Logger
     */
    private Logger logger = Logger.getLogger(getClass());
    
    /**
     * encoding
     */
    private String  encoding = null;

    /**
     * ignore
     */
    private boolean ignore   = true;

    /**
     * @see com.run.common.servlet.TemplateFilter#doPreProcessing(
     *      javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
    public boolean doPreProcessing(ServletRequest request, ServletResponse response) throws IOException, ServletException
    {

        if (ignore || (request.getCharacterEncoding() == null))
        {
            if (encoding != null)
            {
                request.setCharacterEncoding(encoding);
            }
        }
        return true;
    }

    /**
     * @see com.run.common.servlet.TemplateFilter#doPostProcessing(
     *      javax.servlet.ServletRequest, javax.servlet.ServletResponse)
     */
    public void doPostProcessing(ServletRequest request, ServletResponse response) throws IOException, ServletException
    {
    }

    /**
     * @see com.run.common.servlet.TemplateFilter#initFilter(
     *      javax.servlet.FilterConfig)
     */
    public void initParameter(FilterConfig filterConfig) throws ServletException
    {
        encoding = filterConfig.getInitParameter("encoding");
        if ("false".equals(filterConfig.getInitParameter("ignore")))
        {
            ignore = false;
        }
        logger.info("CharacterEncodingFilter initParameter ok.");
    }

    /**
     * @see javax.servlet.Filter#destroy()
     */
    public void destroy()
    {
        encoding = null;
        super.destroy();
    }

}
